﻿using M.ShopMachine.Write.WebApi.Application.Command.WorkshopCommand;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M.ShopMachine.Write.WebApi.Controllers
{
    /// <summary>
    /// 车间api
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WorkshopController : ControllerBase
    {
        private readonly IMediator mediator;

        public WorkshopController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        #region 添加

        /// <summary>
        /// 车间-添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddWorkshopAsync(AddWrokshopCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        #endregion

        #region 修改

        /// <summary>
        /// 修改车间信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdWorkshopAsync(UpdWorkshopCommad command)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            try
            {
                apiResult.Data = await mediator.Send(command);
                return apiResult;
            }
            catch (Exception ex)
            {
                apiResult.Code = 500;
                apiResult.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdStateWorkshopAsync(UpdStateWorkshopCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code=500;
                result.Message = ex.Message;
                throw;
            }
        }

        #endregion
    }
}
